import { defineStore } from 'pinia'
import netconfig from '@/config/netconfig'
import { getAgreementInfo } from '@/api/account'
import type { AgreementInfo } from '@/api/account'

export type State = {
  /**
   * Environment constants
   */
  assetsHost: string
  host: string
  hcaWebsocketUrl: string
  hceWebsocketUrl: string
  versionNo: string
  agreeement: AgreementInfo | null
  /**
   * System info
   */
  systemInfo: UniNamespace.GetSystemInfoResult
  /**
   * The bounding rect of Capsule button (Top-Right buttion)
   */
  capsuleRect: UniNamespace.GetMenuButtonBoundingClientRectRes
  /**
   * Statement
   */
  noNetwork: boolean
}
export type Getters = {
  language(state: State): 'zh_CN' | 'en'
  statusBarHeight(state: State): number
  capsuleBarHeight(state: State): number
  titleBarHeight(state: State): number
  /**
   * Safe Area
   */
  safeAreaInsetTop(state: State): number
  safeAreaInsetBottom(state: State): number
}
export type Actions = {
  loadAgreement(): Promise<void>
  loadSystemInfo(): void
  checkNetwork(): Promise<void>
}

export const useEnvStore = defineStore<'env', State, Getters, Actions>('env', {
  state: () => ({
    assetsHost: netconfig.ASSETSPRE,
    host: netconfig.ROOTURL,
    versionNo: netconfig.VERSION,
    hcaWebsocketUrl: netconfig.WEBSOCKETURL,
    hceWebsocketUrl: netconfig.HcaExtWEBSOCKETURL,
    agreeement: null,
    systemInfo: uni.getSystemInfoSync(),
    capsuleRect: uni.getMenuButtonBoundingClientRect(),
    noNetwork: false,
  }),
  getters: {
    language({ systemInfo }) {
      return systemInfo.language === 'en' ? 'en' : 'zh_CN'
    },
    statusBarHeight({ systemInfo }) {
      return systemInfo.statusBarHeight || 0
    },
    capsuleBarHeight({ capsuleRect }) {
      // return capsuleRect.bottom - capsuleRect.top
      return capsuleRect.bottom + capsuleRect.top - this.statusBarHeight * 2
    },
    titleBarHeight() {
      return this.statusBarHeight + this.capsuleBarHeight
    },
    safeAreaInsetTop({ systemInfo }) {
      return systemInfo.safeAreaInsets?.top || 0
    },
    safeAreaInsetBottom({ systemInfo }) {
      return systemInfo.safeAreaInsets?.bottom || 0
    },
  },
  actions: {
    async loadAgreement() {
      try {
        const res = await getAgreementInfo()
        this.agreeement = {
          TOU: {
            ...res.data.TOU,
            url: this.assetsHost + res.data.TOU.url,
          },
          DPI: {
            ...res.data.DPI,
            url: this.assetsHost + res.data.DPI.url,
          },
          DCBTI: {
            ...res.data.DCBTI,
            url: this.assetsHost + res.data.DCBTI.url,
          },
        }
      } catch (e) {
        console.error(e)
      }
    },
    loadSystemInfo() {
      this.systemInfo = uni.getSystemInfoSync()
    },
    async checkNetwork() {
      try {
        await new Promise((resolve, reject) => {
          uni.getNetworkType({
            success(res) {
              if (res.networkType === 'none') return reject('NetworkType is none')
              return resolve(res.networkType)
            },
            fail(e) {
              console.error(e)
              return reject(e)
            },
          })
        })
        this.noNetwork = false
      } catch (e) {
        console.error(e)
        this.noNetwork = true
      }
    },
  },
})
